// pages/goods-detail/goods-detail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: {},
    productImgList: [],
    detailImgList: [],
    cartCount: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("options: ", options)
    this.getGoodsDetail( options.id );
    this.getCartGoodsAmount();
  },

  addCart( event ){
    let goodsId = event.currentTarget.dataset.id;
    console.log("goodsId: ", goodsId);
    wx.request({
      url: `http://localhost:8082/app/omsGoodsCart`,
      method: "POST",
      data:{
          goodsId,
          count: 1
      },
      success: (res) => {
          console.log("addCart: ",res);
          this.setData({
            cartCount: this.data.cartCount+1
          })
      },
      fail: (err)=>{
        console.error("2222", err)
      }
    })
  },

  getCartGoodsAmount( categoryId ){
    wx.request({
      url: 'http://localhost:8082/app/omsGoodsCart/amount',
      method: "GET",
      data:{
        userId: 1
      },
      success: (res) => {
        res = res.data;
        if( res.code != 200 ){
          return;
        }
        let data = res.data;
        console.log("cartCount: ",data)
        this.setData({
          cartCount: data
        })
      },
      fail: (err)=>{
        console.error("2222", err)
      }
    })
  },


  getGoodsDetail( goodsId ){
    wx.request({
      url: `http://localhost:8082/app/omsGoodsInfo/${goodsId}`,
      method: "GET",
      data:{
          id: goodsId
      },
      success: (res) => {
        res = res.data;
        if( res.code != 200 ){
          return;
        }
        let data = res.data;
        let actualPrice = data.actualPrice+'' || '';
        data = {
          ...data,
          actualIntPrice: actualPrice.substring(0, actualPrice.length-2),
          actualFloatPrice: actualPrice.substring( actualPrice.length-2),
        }
        let productImgList = data.productImgList || [];
        let detailImgList = data.detailImgList || [];
        this.setData({
          goodsInfo: data,
          productImgList: productImgList,
          detailImgList:detailImgList 
        });
        console.log(" data.detailImgList: ",detailImgList)
      },
      fail: (err)=>{
        console.error("2222", err)
      }
    })
  },

  toCart(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },

  toWaiter(){
    wx.makePhoneCall({
      phoneNumber: '19196163234',
      success:()=>{
        console.log("电话拨打成功...")
      },
      error: (()=>{
        console.log("电话拨打失败...")
      })
    })
  },

  toIndex(){
    wx.navigateTo({
      url: '/index/index'
    })
    wx.navigateBack();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})